package countDownLatch;

import java.util.concurrent.CountDownLatch;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-09-26
 * Time: 下午3:50
 * Description: ${description}
 */
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {

        int total =300;
        CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch doneSignal = new CountDownLatch(total);

        for (int i = 0; i < total; i++) {
            new Thread(new Worker(startSignal,doneSignal)).start();
        }

        startSignal.countDown();
        doneSignal.await();
        System.out.println("current thread" +Thread.currentThread());
    }
}


class Worker implements Runnable {
    private final CountDownLatch startSignal;
    private final CountDownLatch doneSignal;

    public Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
        this.startSignal = startSignal;
        this.doneSignal = doneSignal;
    }

    @Override
    public void run() {
        try {
            startSignal.await();
            doWork();
            doneSignal.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doWork() {
        System.out.println(Thread.currentThread());
    }
}

